#include "RenderQueue.h"
RenderQueue::RenderQueue()
	:Component(COMP_RENDER_QUEUE),
	m_vertexLayout(NULL)
{
}

RenderQueue::~RenderQueue()
{
} 

void RenderQueue::AddedToObject()
{
}

void RenderQueue::Initial(const string shader)
{
	SendMessage(MSG_CREATE_EFFECT,AuxGroupArgus(shader,&m_effect));
	m_effect->Initial();
	

	SetPerspMatrix();
	// if(m_vertexShader==NULL)
	// {
		// ID3DBlob* pVSBlob = NULL;
		// SendMessage(MSG_COMPARE_SHARED,AuxGroupArgus(shader,string("VS"),string("vs_4_0"),&pVSBlob));
		// SendMessage(MSG_CREATE_VERTEX_SHADER,AuxGroupArgus(pVSBlob,&m_vertexShader));

		// D3D11_INPUT_ELEMENT_DESC layout[] =
		// {
		    // { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		// };
		// UINT numElements = ARRAYSIZE( layout );

		// //SendMessage(MSG_CREATE_INPUT_LAYOUT,AuxGroupArgus(layout,numElements,pVSBlob,&m_vertexLayout));
		// pVSBlob->Release();
	// }

	// if(m_pixelShader==NULL)
	// {
		// ID3DBlob* pPSBlob = NULL;
		// SendMessage(MSG_COMPARE_SHARED,AuxGroupArgus(shader,string("PS"),string("ps_4_0"),&pPSBlob));
		// SendMessage(MSG_CREATE_PIXEL_SHADER,AuxGroupArgus(pPSBlob,&m_pixelShader));
		// pPSBlob->Release();
	// }	
	
	// if(!m_vertexLayout||
		// !m_vertexShader||
		// !m_pixelShader)
		// EXCEPTION("")
	// Vector<ID3D11ClassInstance*> tmp;
	// 
	// SendMessage(MSG_SET_VSS,AuxGroupArgus(m_vertexShader,tmp));
	// SendMessage(MSG_SET_PSS,AuxGroupArgus(m_pixelShader,tmp));
}

void RenderQueue::Render()
{
	try
	{
		float* color=new float[4];

		// Setup the color to clear the buffer to.
		color[0] = 0.0f;
		color[1] = 1.0f;
		color[2] = 1.0f;
		color[3] = 1.0f;
	
	
		SendMessage(MSG_CLEAR_RTV,AuxGroupArgus(static_cast<ID3D11RenderTargetView*>(NULL),color,true));
		SetViewMatrix();
		int incr=0;
		SendMessage(MSG_TIME_INCR,AuxGroupArgus(&incr));
		for(RenderObjectVect::iterator iter=m_renderQueueMap.begin();iter!=m_renderQueueMap.end();iter++)
		{
			(*iter)->UpdatePreFrame(incr);
			(*iter)->Render();
		}
		delete[] color;
	}
	catch(boost::bad_any_cast& e)
	{
		PRINT_BOOST_ERROR(e)
	}
	catch(std::exception& e)
	{
		PRINT_ERROR(e)
	}

}

void RenderQueue::SetViewMatrix()
{
	XMMATRIX viewMatrix;
	SendMessage(MSG_GET_VIEW_MATRIX,AuxGroupArgus(&viewMatrix));
	m_effect->SetMatrix("ViewMatrix",viewMatrix);
}

void RenderQueue::SetPerspMatrix()
{
	XMMATRIX perspMatrix;
	SendMessage(MSG_GET_PERSP_MATRIX,AuxGroupArgus(&perspMatrix));
	m_effect->SetMatrix("PerspMatrix",perspMatrix);
}

void RenderQueue::RenderSignalObject(RenderObject::sPtr& o)
{
	o->Render();
}

void RenderQueue::AddRenderObject(RenderObject::sPtr objPtr)
{
	objPtr->Initial(m_effect);
	m_renderQueueMap.push_back(objPtr);
}

void RenderQueue::RemoveObject(string str)
{
	for(RenderObjectVect::iterator iter=m_renderQueueMap.begin();iter!=m_renderQueueMap.end();iter++)
	{
		if((*iter)->GetName()==str)
		{
			m_renderQueueMap.erase(iter);
			break;
		}
	}
}

stringstream RenderQueue::Dump()
{
	stringstream ss;
	for(RenderObjectVect::iterator iter=m_renderQueueMap.begin();iter!=m_renderQueueMap.end();iter++)
	{
		ss<<(*iter)->GetName();
	}
	return ss;
}

void RenderQueue::RemoveAllObject()
{
	m_renderQueueMap.clear();
}